package com.trkj.controller.wdm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.trkj.pojo.DefinitionDept;
import com.trkj.pojo.Emppass;
import com.trkj.pojo.StaffInformation;
import com.trkj.service.ServiceException;
import com.trkj.service.mapper.Dutyservice;
import com.trkj.service.mapper.TongJiService;
import com.trkj.service.wdm.EmpPassService;
import com.trkj.service.wdm.StaffService;
import com.trkj.vo.Result2;
import com.trkj.vo.ShouYeVo;


@Controller
@SessionAttributes({"staff","funIds","sy","gryy","jtyy","gskc","qtyy"})
public class LoginController {
	@Autowired
	private EmpPassService emppass;
	@Autowired
	private Dutyservice dservice;
	@Autowired
	private StaffService staffservice;
	@Autowired
	private TongJiService tjs;
	// 定义部门分页查询
			@RequestMapping("select_definitiondep")
			@ResponseBody
			public Map select_definitiondep(int page,int rows) {
				Page<JSONArray> p =PageHelper.startPage(page,rows);
				dservice.findAll();
				List<JSONArray> list = p.getResult();
				Map map = new HashMap<>();
				map.put("total", p.getTotal());
				map.put("rows", list);
				return map;
			}
	
	
	
	//登录
	@GetMapping("to_Login")
	public String to_Login(){
		return "login";
	}
	
	/**
	 * 登录验证
	 * @param name
	 * @param pwd
	 * @param m
	 * @return
	 */
	@RequestMapping("login_yanzheng")
	public String login(String name,String pwd,Model m){
		try {
			if(emppass.LoginVerify(name, pwd)){
				StaffInformation s =emppass.user(name);
				if(s!=null){
					m.addAttribute("staff",s); 
					Long emps=emppass.select(name);
					m.addAttribute("funIds",emppass.findAllFunction(emps));
					if(staffservice.querysy(s.getStainId())!=null){
						List<ShouYeVo> syvo =staffservice.querysy(s.getStainId());
						m.addAttribute("sy",syvo); 
					}
					
					//统计
					JSONObject zjls = tjs.zongJiLuShu();
					JSONObject gryx = tjs.gryy();
					Object a =gryx.get("GRYY");
					if(a==null){
						a=0;
					}
					double l =Double.valueOf(String.valueOf(a)).doubleValue();
					Object aa =zjls.get("ZJLTS");
					double ll =Double.valueOf(String.valueOf(aa)).doubleValue();
					double lll =l/ll*100;
					
					JSONObject jtyy = tjs.jtyy();
					Object b =jtyy.get("JTYY");
					if(b==null){
						b=0;
					}
					double bb =Double.valueOf(String.valueOf(b)).doubleValue();
					double bbb=bb/ll*100;
					
					JSONObject gskc = tjs.gskc();
					Object c =gskc.get("GSKC");
					if(c==null){
						c=0;
					}
					double cc =Double.valueOf(String.valueOf(c)).doubleValue();
					double ccc=cc/ll*100;
					
					
					JSONObject qtyy = tjs.qtyy();
					Object d =gskc.get("QTYY");
					if(d==null){
						d=0;
					}
					double dd =Double.valueOf(String.valueOf(d)).doubleValue();
					double ddd=dd/ll*100;
					
					m.addAttribute("gryy",lll); 
					m.addAttribute("jtyy",bbb); 
					m.addAttribute("gskc",ccc); 
					m.addAttribute("qtyy",ddd); 
				} 
				
				return "redirect:/to_index";
			}else{
				String ss ="用户名密码错误或没有登陆权限";
				m.addAttribute("pd", ss);
			
				return "login";
			}
			
		} catch (ServiceException e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	@RequestMapping("sy")
	@ResponseBody
	public List<ShouYeVo> sy(HttpSession session){
		StaffInformation saff = (StaffInformation) session.getAttribute("staff");
		List<ShouYeVo> syvo =staffservice.querysy(saff.getStainId());
		for (ShouYeVo shouYeVo : syvo) {
			System.out.println(shouYeVo.getDefideName());
		}
		return syvo;
	}
	
	/**
	 * 注销
	 * @param 
	 * @return
	 */
	@RequestMapping("zhuxiao")
	public String zhuXiao(SessionStatus s){
		s.setComplete();
		return "redirect:/to_Login";
	}
	//修改密码
	@RequestMapping("xgygmm")
	@ResponseBody
	public Result2 xgmm(Long stainId,String jiumima,String xinmima){
		try {
			staffservice.xiugaimima(stainId, jiumima, xinmima);
			return Result2.SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result2.failure(e.getMessage());
		}
			
	}
}
